package com.mo.common;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.alibaba.fastjson.JSONObject;
import com.mo.config.properties.BaseInfoProperties;
import com.mo.config.properties.TvPrefixProperties;

@SuppressWarnings("deprecation")
public class TvBaseController {

	@Autowired
	protected TvPrefixProperties tvPrefixProperties;
	@Autowired
	protected BaseInfoProperties baseInfoProperties;
	@Autowired
    StringRedisTemplate stringRedisTemplate;
	
	private static Logger logger = Logger.getLogger(TvBaseController.class);
	  public Map<String,Object> writeSuccessToClient(String msg,Object data){
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	map.put("res", 1);
			map.put("msg", msg);
			map.put("data",data );
//			return ResponseEntity.ok(map);
			return map;
	    	
		}
   
	public Map<String,Object> writeExceptionToClient(String stackTrace){
		Map<String,Object> map = new HashMap<String,Object>();
    	map.put("res", -1);
		map.put("msg", "系统异常");
		map.put("data",stackTrace );
//		return ResponseEntity.ok(map);
		return map;
	}
	public Map<String,Object> writeErrorToClient(String msg){
		Map<String,Object> map = new HashMap<String,Object>();
    	map.put("res", 0);
		map.put("msg", msg);
//		return ResponseEntity.ok(map);
		return map;
	}
	/*private void writeJson(HttpServletResponse response,String jsonStr){
		try {
			response.setContentType("text/json");
			response.setContentType("application/json");
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(jsonStr);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			log.error("网络异常", e);
		}
	}*/
	
	
	@SuppressWarnings("resource")
	public static JSONObject doPost(String url,JSONObject json,String token){
		logger.info("adurl="+url);
		logger.info("adparam="+json);
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		try {
			StringEntity s = new StringEntity(json.toString(),Charset.forName("UTF-8"));
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");//发送json数据需要设置contentType
			post.setEntity(s);
			post.setHeader("token_id", token);
			post.setHeader("Content-Type", "application/json; charset=UTF-8");  
			HttpResponse res = client.execute(post);
			if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity httpEntity = res.getEntity();
//				String result = EntityUtils.toString(res.getEntity());// 返回json格式：
//				response = JSONObject.fromObject(result);
				BufferedReader reader = null;

                    reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 10 * 1024);
                    StringBuilder strBuilder = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        strBuilder.append(line);
                    }
                    // 用fastjson的JSON将返回json字符串转为json对象
                    //response = JSONObject.fromObject();
                    response = JSONObject.parseObject(strBuilder.toString());
			}
		} catch (Exception e) {
			return null;
//			throw new RuntimeException(e);
		}
		return response;
	}
	
}
